/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/
#include "trackaway.h"

#include "controls/ftmessagebox.h"
#include "controls/ftvboxlayout.h"
#include "controls/fthboxlayout.h"

#include "dialogconfig.h"
#include "dialogabout.h"

#include "configuration.h"

#include "livetrack.h"

#include "tracksmanager.h"
#include "gpsmanager.h"
#include "tracktable.h"
#include "mapsmanager.h"
#include "statusbar.h"
#include "systemcontrols.h"

TrackAway::TrackAway() :
    RotatingDialog()
{
    // Quit when last windows has been closed
    connect( qApp, SIGNAL(lastWindowClosed()),
             qApp, SLOT(quit()));

    connect( qApp, SIGNAL(aboutToQuit()),
             this, SLOT(prepareToQuit()));

    // Table, GPS and LayerManager are inside a QStackedWidget:
    center = new QStackedWidget( this );

    createLivetrack();

    createGpsManager();

    createTracksManager();

    createMapsManager();

    createTrackingTable();

    createSystemControls();

    createStatusBar();

    center->addWidget( gpsManager );
    center->addWidget( livetrack );
    center->addWidget( mapsManager );
    center->addWidget( tracksManager );
    center->addWidget( systemControls );
    center->addWidget( trackingTable );

    center->setCurrentWidget( trackingTable );
}

TrackAway::~TrackAway()
{  
}

void TrackAway::closeEvent(QCloseEvent * event)
{
    if ( FTMessageBox::Yesno(tr("Quit Track@Way..."),
                             tr("Really close?") ) )
        event->accept();
    else
        event->ignore();
}

void TrackAway::createLivetrack()
{
    livetrack = new LiveTrack();
    connect( qApp, SIGNAL(aboutToQuit()),
             livetrack, SLOT(prepareToQuit()));
    connect( configuration, SIGNAL(trackingModeChanged(Configuration::tracking_mode,int)),
             livetrack, SLOT(trackingModeChanged(Configuration::tracking_mode,int)));
    connect( livetrack, SIGNAL(livetrackPopup()),
             this, SLOT(showLivetrack()));
}

void TrackAway::prepareToQuit()
{
    configuration->SaveConfig();
}

void TrackAway::createTracksManager()
{
    // Create the tracks manager
    tracksManager = new TracksManager( gpsManager );

    connect( livetrack, SIGNAL(setDestination()),
             tracksManager, SLOT(stopNavigation()) );
}

void TrackAway::createMapsManager()
{
    // Create the tracks manager
    mapsManager = new MapsManager();
}

void TrackAway::createTrackingTable()
{
    // Create the tracking table, which is the default opened widget
    trackingTable = new TrackTable(tracksManager, livetrack, mapsManager);

    // Connect GPS position update to the table
    connect( gpsManager, SIGNAL(gpsUpdate(GPS_point)),
            trackingTable, SLOT(gpsUpdated(GPS_point)) );
    connect( gpsManager, SIGNAL(gpsStatusChange(GpsManager::gps_status)),
            trackingTable, SLOT(gpsStatusChanged(GpsManager::gps_status)) );
    connect( livetrack, SIGNAL(setDestination(QGeoCoordinate)),
             trackingTable, SLOT(startNavigation(QGeoCoordinate)));
    connect( mapsManager, SIGNAL(enableMaps(bool)),
             trackingTable, SLOT(mapsEnabled(bool)));
    connect( livetrack, SIGNAL(trackingStatusChanged(bool)),
             trackingTable, SLOT(livetrackRecording(bool)) );

    connect( trackingTable, SIGNAL(toggleTracking()),
             livetrack, SLOT( toggleTracking()) );
    connect( trackingTable, SIGNAL(toggleMaps()),
             mapsManager, SLOT( toggleEnableMap()) );
    connect( trackingTable, SIGNAL(toggleGps()),
             gpsManager, SLOT( gpsStatusToggle()) );

    connect( tracksManager, SIGNAL(navigateTo(QGeoCoordinate)),
             trackingTable, SLOT(startNavigation(QGeoCoordinate)) );
    connect( trackingTable, SIGNAL(navigationStopped()),
             tracksManager, SLOT(stopNavigation()));
    connect( trackingTable, SIGNAL(destinationReached()),
             tracksManager, SLOT(destinationReached()));
}

void TrackAway::createGpsManager()
{
    // Create the GPS page
    gpsManager = new GpsManager();
    connect( gpsManager, SIGNAL(gpsUpdate(GPS_point)),
            livetrack, SLOT(addPointFromGps(GPS_point)) );

}

void TrackAway::createSystemControls()
{
    // Create the selector page
    systemControls = new SystemControls();
    // set as default widget

    connect( systemControls, SIGNAL(showAbout()),
             this, SLOT(showAbout()) );

    connect( systemControls, SIGNAL(showConfig()),
             this, SLOT(showConfig()) );

    connect( systemControls, SIGNAL(askQuit()),
             this, SLOT(askQuit()) );
}

void TrackAway::createStatusBar()
{
    statusBar = new StatusBar( gpsManager->gpsStatus(), false, mapsManager->mapsEnabled() );

    // show widgets...
    connect( statusBar, SIGNAL(clicked(int)),
             this, SLOT(showWidget(int)));

    // Update the status bar tracking status (background color)
    connect( livetrack, SIGNAL(trackingStatusChanged(bool)),
             statusBar, SLOT(updateTrackingStatus(bool)));

    // Update the status bar GPS status (background color)
    connect( gpsManager, SIGNAL(gpsStatusChange(GpsManager::gps_status)),
             statusBar, SLOT(updateGpsStatus(GpsManager::gps_status)) );

    connect( mapsManager, SIGNAL(enableMaps(bool)),
             statusBar, SLOT(updateMapsStatus(bool)) );
}

void TrackAway::showWidget(int w)
{
    if ( w == center->currentIndex() )
    {
        center->setCurrentIndex( BUTTON_TABLE );
        statusBar->selectButton( BUTTON_TABLE );
    }
    else
    {
        center->setCurrentIndex( w );
        statusBar->selectButton( w );
    }
}

void TrackAway::showLivetrack()
{
    statusBar->selectButton( BUTTON_LIVE );
    center->setCurrentWidget( livetrack );
}

void TrackAway::showAbout()
{
    DialogAbout about;
    about.showFullScreen();
    about.exec();
}

void TrackAway::showConfig()
{
    DialogConfig config;
    config.showFullScreen();
    config.exec();
}

void TrackAway::askQuit()
{
    if ( FTMessageBox::Yesno(tr("Quit Track@Way..."),
                             tr("Really close?") ) )
        qApp->exit(0);
}

QLayout* TrackAway::createVertical()
{
    // Build FreeTrack layout...
    FTVBoxLayout* grid = new FTVBoxLayout();
    // Add center widget
    grid->addWidget(center);
    // Add status-bar
    statusBar->setOrientation( FTControls::horizontal );
    grid->addWidget( statusBar );
    return grid;
}


QLayout* TrackAway::createHorizontal()
{
    // Build FreeTrack layout...
    FTHBoxLayout* grid = new FTHBoxLayout();
    // Add center widget
    grid->addWidget(center);
    // Add status-bar
    statusBar->setOrientation( FTControls::vertical );
    grid->addWidget( statusBar );
    return grid;
}
